package com.system.dao.UserDao;

import com.system.dao.BaseDao;
import com.system.pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao{
    @Override
    public User Login(Connection connection, String UserCode, String UserPsw) throws SQLException {
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        User user = null;

        //首先先判断connection，其次在获取数据和执行SQL语句
        if(connection!=null){
            String sql = "select * from user where UserCode=? and UserPsw=?";
            Object[] o = {UserCode,UserPsw};
            resultSet = BaseDao.excute(connection,statement,resultSet,sql,o);
            while(resultSet.next()){
                user = new User();
                user.setUserId(resultSet.getInt("UserId"));
                user.setUserCode(resultSet.getString("UserCode"));
                user.setUserPsw(resultSet.getString("UserPsw"));
                user.setUserGetNub(resultSet.getInt("UserGetNub"));
                user.setUserLossNub(resultSet.getInt("UserLossNub"));
                user.setUserImgPath(resultSet.getString("UserImgPath"));
            }
        }
        BaseDao.closeResource(null,statement,resultSet);
        return user;
    }

    /*
    *注册功能
    */
    @Override
    public int Register(Connection connection, String UserCode, String UserPsw, int UserLossNub, int UserGetNub, String UserImgPath) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int Rows = 0;
        if (connection != null){
            String sql = "insert into user(UserCode,UserPsw,UserLossNub,UserGetNub,UserImgPath) values(?,?,?,?,?)";
            Object[] o = {UserCode,UserPsw,UserLossNub,UserGetNub,UserImgPath};
            Rows = BaseDao.excute(connection,preparedStatement,sql,o);
        }
        System.out.println("ROWS  ---->  "+Rows);
        return Rows;
    }

    /*
    * 注册是否用重复的用户名
    * */
    @Override
    public boolean IfRepetition(Connection connection, String UserCode) throws SQLException {
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        boolean flag = true;
        if (connection != null) {
            String sql = "select UserCode from user where UserCode = ?";
            Object[] o = {UserCode};
            resultSet = BaseDao.excute(connection,statement,resultSet,sql,o);
            if(resultSet.next()){
                //有重复的用户
                System.out.println("有重复");
                flag = true;
            }else{
                System.out.println("无重复");
                flag = false;
            }
            BaseDao.closeResource(null,statement,resultSet);
        }
        return flag;
    }

    @Override
    public int UpdatePsw(Connection connection, String updatePsw ,int UserId) throws SQLException {
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int flag = 0;
        if (connection != null){
            String sql = "update user set UserPsw=? where UserId=?";
            Object[] o = {updatePsw,UserId};
            flag = BaseDao.excute(connection,statement,sql,o);
        }
        System.out.println("flag  ---->  "+flag);
        return flag;
    }


}
